//
//  WelcomeViewController.swift
//  sinazd
//
//  Created by apple on 16/3/10.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backImgView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
        
    }
    
    
    
    
    //添加界面控件并设置约束等
    func setUpUI() {
        
        view.addSubview(iconImgView)
        view.addSubview(welcomeLabel)
        
        
        iconImgView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(80)
            make.height.equalTo(80)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImgView.snp_centerX)
            make.top.equalTo(iconImgView.snp_bottom).offset(16)
        }
        
        
        //设置图片
        iconImgView.sd_setImageWithURL(UserAccount.sharedAccount.headImageURL)
        //裁剪成圆形
        iconImgView.layer.cornerRadius = 40
        iconImgView.layer.masksToBounds = true
        iconImgView.layer.borderWidth = 3
        iconImgView.layer.borderColor = UIColor.redColor().CGColor
        
    }
    
    
    
    

   //懒加载界面上的控件
    private lazy var backImgView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImgView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var welcomeLabel:UILabel = {
        
       let l = UILabel()
        
        l.text = (UserAccount.sharedAccount.name ?? "") + "  欢迎归来"
        
        l.font = UIFont.systemFontOfSize(16)
        
        l.textColor = UIColor.darkGrayColor()
        
        l.alpha = 0
        
        l.sizeToFit()
        
        
        return l
        
        
    }()
    
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        startAnimation()
    }
    
    //动画设置
    func startAnimation() {
        
        let offsetY = 180 - view.bounds.height
        
        iconImgView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(offsetY)
        }
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("ok")
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "oauth")
                        
                        
                })
        }
        
        
    }
    
    
 
    
}
